<script setup>
import { getHomeBannerAPI } from '@/api/home'
import { onMounted, ref } from 'vue';
import { useCategoryStore } from '@/stores/category';

const categoryStore = useCategoryStore()

const bannerList = ref([])

const getBannerList = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res
}

onMounted(() => {
  getBannerList()
})


</script>

<template>
<div class="banner">
  <div class="container">
    <el-carousel height="500px">
      <el-carousel-item v-for="item in bannerList" :key="item.id">
        <router-link :to="item.hrefUrl">
          <img :src="item.imgUrl" alt="">
        </router-link>
      </el-carousel-item>
    </el-carousel>
    <div class="category">

      <div v-for="item in categoryStore.category" :key="item.id" class="sublist">
        <router-link class="name" :to="`/category/${item.id}`">{{ item.name }}</router-link>
        <router-link v-for="i in item.children.slice(0, 2)" :key="i" to="/">{{ i.name }}</router-link>
        <!-- 弹层渲染位置 -->
        <div class="layer">
          <h3>分类推荐
            <small>根据您的购买或浏览记录推荐</small>
          </h3>
          <ul class="list">
            <li class="item" v-for="good in item.goods" :key="good.id">
              <router-link :to="`/detail/${good.id}`">
                <img :src="good.picture" alt="">
                <div class="info">
                  <p class="name ellipsis-2">{{ good.name }}</p>
                  <p class="desc ellipsis">{{ good.desc }}</p>
                  <p class="price">{{ good.price }}</p>
                </div>
              </router-link>
            </li>
          </ul>
        </div>
      </div>

    </div>
  </div>
</div>
</template>

<style lang="less" scoped>
.container {
  position: relative;
  .category {
    position: absolute;
    top: 0;
    left: 0;
    width: 250px;
    height: 500px;
    background-color: rgba(0, 0, 0, .8);
    .sublist {
      height: 55px;
      line-height: 55px;
      padding-left: 40px;
      a {
        color: #fff;
        margin-right: 4px;
      }
      .name {
        font-size: 16px;
      }
      &:hover {
        background-color: @xtxColor;
        .layer {
          display: block;
        }
      }
    }
    .layer {
      position: absolute;
      top: 0;
      left: 250px;
      height: 500px;
      width: 990px;
      background-color: #fff;
      padding: 0 15px;
      display: none;
      h3 {
        font-size: 20px;
        font-weight: 400;
        line-height: 80px;
        small {
          font-size: 16px;
          color: #666;
        }
      }
      .list {
        display: flex;
        flex-wrap: wrap;
        .item {
          width: 310px;
          height: 120px;
          padding-left: 40px;
          margin-bottom: 15px;
          margin-right: 15px;
          border: 1px solid #eee;
          border-radius: 4px;
          &:nth-child(3n) {
            margin-right: 0;
          }
          a {
            display: flex;
            justify-content: space-between;
            width: 100%;
            height: 100%;
            padding: 10px;
            &:hover {
              background: #e3f9f4;
            }
            img {
              width: 95px;
              height: 95px;
            }
            .info {
              margin-left: 10px;
              width: 155px;
              display: flex;
              flex-direction: column;
              justify-content: space-between;
              .name {
                font-size: 16px;
                line-height: 1;
                color: #666;
              }
              .desc {
                color: #999;
                line-height: 1;
              }
              .price {
                font-size: 22px;
                line-height: 1;
                color: @priceColor;
              }
            }
          }
        }
      }
    }
  }
}
</style>
